package com.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 访问ice接口类(获取相应的小区楼栋信息)
 * @author wwl
 *
 */
public class ListString {
	
	public static List<String> list(String GET_URL){
		 List<String> list = new ArrayList<>();
		 try {
	            URL url = new URL(GET_URL);    
	            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	            connection.setRequestMethod("GET");  
	            connection.connect();
	            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));  
	            String line;  
	            StringBuilder sb = new StringBuilder();  
	            while ((line = br.readLine()) != null) {
	                sb.append(line);  
	            }  
	            br.close();
	            connection.disconnect();
	            new JSONObject();
				JSONObject json=JSONObject.fromObject(sb.toString());
				System.out.println("==json=="+json);
				JSONObject content = json.getJSONObject("content");
				JSONObject contentJson = JSONObject.fromObject(content);
				JSONObject housetype = contentJson.getJSONObject("housetype");
				Iterator it = housetype.keys();  
	            String key="";
	            String value="";
	           
	            while(it.hasNext()){  
	                key = (String) it.next();
	                value = housetype.getString(key);
	                System.out.println("key:"+key+"\t value:"+value);
	                list.add(value);
	            }  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
       return list;
	}
	
	public static List<String> list1(String GET_URL){
		 List<String> list = new ArrayList<>();
		 try {
	            URL url = new URL(GET_URL);    
	            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	            connection.setRequestMethod("GET");  
	            connection.connect();
	            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));  
	            String line;  
	            StringBuilder sb = new StringBuilder();  
	            while ((line = br.readLine()) != null) {
	                sb.append(line);  
	            }  
	            br.close();
	            connection.disconnect();
	            new JSONObject();
				JSONObject json=JSONObject.fromObject(sb.toString());
				System.out.println("==json=="+json);
				String content=json.getString("content");
				JSONArray contentjson = JSONArray.fromObject(content ); 
				for(int i=0;i<contentjson.size();i++){
				    JSONObject job = contentjson.getJSONObject(i);  	// 遍历 jsonarray 数组，把每一个对象转成 json 对象
				    String value=(String) job.get("buildingName");  	// 得到 每个对象中的属性值
				    System.out.println(value);
				    list.add(value);
				 }
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
      return list;
	}
}

